terraform {
  required_providers {
    kubernetes = {
      source  = "hashicorp/kubernetes"
      version = ">= 2.0.0"
    }
  }
}
provider "kubernetes" {
  config_path = "~/.kube/config"
}

variable "namespace" {
  default = "nginx"
}

variable "app" {
  default = "nginx"
}

variable "image" {
  default = "nginx"
}

resource "kubernetes_namespace" "test" {
  metadata {
    name = var.namespace
  }
}
resource "kubernetes_deployment" "test" {
  metadata {
    name      = var.app
    namespace = kubernetes_namespace.test.metadata.0.name
  }

  spec {
    replicas = 1
    selector {
      match_labels = {
        app = var.app
      }
    }
    template {
      metadata {
        labels = {
          app = var.app
        }
      }
      spec {
        container {
          name  = var.app
          image = var.image
          port {
            container_port = 80
          }
          env {
            name = "JAVA_OPTS"
            value = "-server"
          }
          env {
            name = "A"
            value = "AA"
          }
        }
      }
    }
  }
}
resource "kubernetes_service" "test" {
  metadata {
    name      = var.app
    namespace = kubernetes_namespace.test.metadata.0.name
  }
  spec {
    selector = {
      app = kubernetes_deployment.test.spec.0.template.0.metadata.0.labels.app
    }
    type = "ClusterIP"
    port {
      port        = 80
      target_port = 80
    }
  }
  depends_on = [kubernetes_deployment.test]
}